import requests
from bs4 import BeautifulSoup
import re
import itertools
import csv
import codecs

list1=[]
csvfile=open(r"c:\test.csv","w",encoding='utf-8',newline='')
writer=csv.writer(csvfile)
writer.writerow(['标题','时间','场馆','票价','状态'])
for i in itertools.count(1):
    url='https://www.damai.cn/ajax/cityCategoryProjectList.html?pageidx={}&categoryId=1&host=bj&sortType=0&startDate=&endDate=&sortKey=0&t=0.29954403708688915'.format(i)
    r=requests.get(url)
    soup=BeautifulSoup(r.text,'lxml')
    tags=soup.select('li')
    if tags==[]:break
    for tag in tags:
        list1.append(tag.select("div > h5 > a")[0].string)
        list1.append(tag.select("div > p:nth-of-type(1) > span.city-time > strong")[0].get_text())
        list1.append(tag.select("div > p:nth-of-type(1) > span.city-veune > a")[0].get_text())
        list1.append(tag.select("div > div.city-price > div")[0].get_text())
        list1.append(tag.select("div > p.city-state > span")[0].get_text())
        print(tag.select("div > h5 > a")[0])
        writer.writerow(list1)
        list1=[]
        writer.writerow(list1)

csvfile.close()
